//
//  SSRequestProduct.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/2/27.
//

import UIKit
import SwiftyJSON

class SSProductRequest: NSObject {
    //MARK: -networing
    //alllist
    class func getProductRequest(parameters: [String:AnyObject]?,finished:@escaping (_ items: Array<ListItemCombo>) -> Void) {
        SSProgressHUD.share.show()
        HttpConnectTools.share.request(urlString: APIUrl.share.getItemAndCombo_URL, parameters: parameters) { (result, error) in
            SSProgressHUD.share.dismiss()
            if let _ = result{
                let json = JSON(parseJSON: result!)
                let data = json["data"]["listItemCombo"].arrayValue
                var items: Array<ListItemCombo> = []
                data.forEach{
                    (num) in
                    let obj = ListItemCombo()
                    obj.name = num["name"].stringValue
                    var subDatas: Array<Any> = []
                    let subobj = num["listCombo"].arrayValue
                    subobj.forEach { (num1) in
                        let newn = parseJson(dic: num1)
                        subDatas.append(newn)
                    }
                    obj.listCombos = subDatas
                    items.append(obj)
                }
                finished(items)
            }else{
                if let message = error?.userInfo["errorinfo"]{
                    UIViewController.current().view.makeToast(message as? String)
                }
            }
        }
    }
    //singlelist
    class func getSingleProductRequest(parameters: [String:AnyObject]?,finished:@escaping (_ items: Array<ListCombo>) -> Void) {
        SSProgressHUD.share.show()
        HttpConnectTools.share.request(urlString: APIUrl.share.getItemCombo_URL, parameters: parameters) { (result, error) in
            SSProgressHUD.share.dismiss()
            if let _ = result{
                let json = JSON(parseJSON: result!)
                let data = json["data"]["listCombo"].arrayValue
                var items: Array<ListCombo> = []
                data.forEach{
                    (num) in
                    let newn = parseJson(dic: num)
                    items.append(newn)
                }
                finished(items)
            }else{
                if let message = error?.userInfo["errorinfo"]{
                    UIViewController.current().view.makeToast(message as? String)
                }
            }
        }
    }
    
   class func parseJson(dic:JSON) -> ListCombo {
        let newn = ListCombo()
        newn.name = dic["name"].stringValue
        newn.imgUrl = dic["imgUrl"].stringValue
        newn.fee = dic["fee"].stringValue
        newn.id = dic["id"].stringValue
        newn.isSpecs = dic["isSpecs"].stringValue
        newn.specsCount = dic["specsCount"].stringValue
        newn.illustration = dic["illustration"].stringValue
       newn.isDetails = dic["isDetails"].stringValue
       
       
       let size = CGSize(width: ScreenWidth - 16 - 136, height: CGFloat.zero)
       let titleHeight = newn.illustration.sizeWithText(font: UIFont.systemFont(ofSize: 10), size: size).height
       if newn.illustration.count == 0 {
           newn.cellHeight = 40 + 80 + 25
       }else{
           newn.cellHeight = (102 + 12 + 10 + titleHeight)
       }
        return newn
    }
    
}
